package cn.glutnn.service.impl;

import cn.glutnn.entity.dto.DiscussesDTO;
import cn.glutnn.mapper.DiscussMapper;
import cn.glutnn.service.DiscussService;
import cn.glutnn.utils.ThreadLocalUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.glutnn.entity.Discuss;

import java.sql.Date;
import java.util.List;
import java.util.Map;

/**
 * (Discuss)表服务实现类
 *
 * @author msh
 * @since 2024-10-25 09:32:29
 */
@Service("/discussService")
public class DiscussServiceImpl extends ServiceImpl<DiscussMapper, Discuss> implements DiscussService {
    @Autowired
    DiscussMapper discussMapper;

    // 根据知识id查询评论
    @Override
    public List<Discuss> selectByKnowledge(Integer id) {
        return discussMapper.selectByKnowledgeId(id);
    }

    @Override
    public void addDiscussByKnowledge(DiscussesDTO dto) {
//        QueryWrapper<Discuss> queryWrapper = new QueryWrapper<>();
        Integer knowledgeId = dto.getKnowledgeId();//知识id
        String content = dto.getContent();//评论
        //从ThreadLocal中获取用户名
        Map<String, Object> usermap = ThreadLocalUtil.get();
        String username = (String) usermap.get("username");
        Discuss discuss = new Discuss();
        discuss.setKnowledgeId(knowledgeId);
        discuss.setContent(content);
        discuss.setOwnName(username);
        discuss.setCreateTime(new Date(System.currentTimeMillis()));
        save(discuss);
    }
}

